﻿using System.Collections.Generic;
using System.Web.Mvc;
using BostonAzureSite.ViewModels;
using BostonAzureSiteWebRole.Models;

namespace BostonAzureSiteWebRole.Controllers
{
    public class EventsController : Controller
    {
        //
        // GET: /Events/

        public ActionResult All()
        {
            var db = new BostonAzureEventRepository();

            var allEvents = db.FindAllEvents();

            // could use AutoMapper to populate ViewModel objects

            var eventList = new List<EventsViewModel>();
            foreach (var ue in allEvents)
            {
                var evm = new EventsViewModel(ue.title, ue.description_html, ue.start_utc, ue.end_utc);
                eventList.Add(evm);
            }

            return View(eventList);
        }

        public ActionResult Upcoming()
        {
            var db = new BostonAzureEventRepository();

            var upcomingEvents = db.FindUpcomingEvents();
            
            // could use AutoMapper to populate ViewModel objects

            var eventList = new List<EventsViewModel>();
            foreach (var ue in upcomingEvents)
            {
                var evm = new EventsViewModel(ue.title, ue.description_html, ue.start_utc, ue.end_utc);
                eventList.Add(evm);
            }

            return View(eventList);
        }

        public ActionResult Next()
        {
            var db = new BostonAzureEventRepository();
            var evt = db.FindNextEvent();
            return View(evt == null ? "NEXT MEETING TOPIC(S) WILL BE ANNOUNCED SOON" : evt.description_html);
        }

        public ActionResult Past()
        {
            return Index();
        }

        //
        // GET: /Events/

        public ActionResult Index()
        {
            return All();
        }

        //
        // GET: /Events/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Events/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Events/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Events/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Events/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
